package com.example.bookstore.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.bookstore.common.BaseContext;
import com.example.bookstore.common.PageResult;
import com.example.bookstore.common.Result;
import com.example.bookstore.entity.Admin;
import com.example.bookstore.entity.Book;
import com.example.bookstore.entity.User;
import com.example.bookstore.entity.User;
import com.example.bookstore.exception.CustomException;
import com.example.bookstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;
    @PostMapping("/page")
    public PageResult<User> selectByPageAndCondition(int currentPage, int pageSize,@RequestBody User user){
        PageResult<User> pageResult = userService.selectByPageAndCondition(currentPage, pageSize, user);
        return pageResult;
    }
    @GetMapping("/byUsername/{username}/{id}")//新增和修改用户名时查看是否已存在该用户
    public User selectByUsername(@PathVariable String username,@PathVariable int id){
        User user = userService.selectByUsername(username,id);
        return user;
    }
    @PutMapping
    public Result<String> addUser(@RequestBody User user){
        Result<String> stringResult = userService.addUser(user);
        return stringResult;
    }
    @PostMapping("/update")
    public Result<String> updateUser(@RequestBody User user, HttpServletRequest request){
        User updateUser= userService.updateUser(user);
        request.getSession().setAttribute("user",updateUser);
        return Result.success("更新成功");
    }

    @PostMapping("/update/image")
    public Result<String> updateAvatar(@RequestBody User user, HttpServletRequest request){
        User updateUser= userService.updateAvatar(user);
        request.getSession().setAttribute("user",updateUser);
        return Result.success("更新头像成功");
    }
    @DeleteMapping
    public Result<String> deleteUser(@RequestBody List<Long> ids){
        Result<String> stringResult = userService.deleteUser(ids);
        return stringResult;
    }
    @PostMapping("/enable")
    public Result<String> enable(Integer id){
        Result<String> stringResult = userService.enable(id);
        return stringResult;
    }
    @PostMapping("/disable")
    public Result<String> disable(Integer id){
        Result<String> stringResult = userService.disable(id);
        return stringResult;
    }
    @PostMapping("/login")
    public Result<String> login(@RequestBody User user, HttpServletRequest request){
        String username = user.getUsername();
        String password = user.getPassword();
        User userReturn = userService.login(username, password);
        if (userReturn==null) {
            return Result.error("登录失败");
        }
        if (userReturn.getStatus()==0){
            return Result.error("该账号已禁用");
        }
        request.getSession().setAttribute("user",userReturn);
        String jsonUser= JSON.toJSONString(userReturn);
        redisTemplate.opsForValue().set(username,jsonUser,5, TimeUnit.MINUTES);//会话5分钟过期
        return Result.success("登录成功");
    }
    @GetMapping
    public User getUser(HttpServletRequest request){
        Object ob= request.getSession().getAttribute("user");
        User user=(User) ob;
        String username = user.getUsername();
        String jsonString = (String)  redisTemplate.opsForValue().getAndExpire(username,5,TimeUnit.MINUTES);//获取同时更新缓存时间
        if (jsonString==null){
            request.getSession().removeAttribute("user");
            return null;
        }
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        User redis = JSON.toJavaObject(jsonObject,User.class);
        return redis;
    }
    @PostMapping("/logout")
    public Result<String> logout(HttpSession session){
        User user = (User) session.getAttribute("user");
        redisTemplate.delete(user.getUsername());
        session.removeAttribute("user");
        return Result.success("退出成功");
    }
    @PostMapping("/register")
    public Result<String> register(@RequestBody Map<String,String> param){
        Result<String> register = userService.register(param);
        return register;
    }

}
